﻿// (c)2010 David Schwarz (david.schwarz@live.com)
// This file is licensed under GNU GPL v2 license, see more at http://www.gnu.org/licenses/gpl-2.0.html
namespace vKapse.DDTool.Core
{

	#region Using

	using System;
	using System.Collections.Generic;
	using System.Text;

	#endregion Using

	/// <summary>
	/// Manažer triggerů
	/// </summary>
	internal class TriggerManager
	{

		#region Members

		private static volatile TriggerManager _instance = null;
		private static object _syncRoot = new object();

		private List<TriggerAssociationBase> m_associations;
		
		#endregion Members

		#region Constructor

		/// <summary>
		/// Konstruktor
		/// </summary>
		protected TriggerManager()
		{
			this.m_associations = new List<TriggerAssociationBase>();
		}

		#endregion Constructor

		#region Properties

		/// <summary>
		/// Vrací instanci této třídy
		/// </summary>
		public static TriggerManager Instance
		{
			get
			{
				if (_instance == null)
				{
					lock (_syncRoot)
					{
						if (_instance == null)
						{
							_instance = new TriggerManager();
						}
					}
				}
				return _instance;
			}
		}

		#endregion Properties

		#region Methods

		public TriggerAssociationBase Register(TriggerBase trigger, object obj)
		{
			Type type = obj.GetType();
			TriggerAssociationBase found = this.m_associations.Find(delegate(TriggerAssociationBase match)
				{
					return match.Type == type;
				});
			if (found != null)
			{
				found.Register(trigger, obj);
				return found;
			}
			return null;
		}

		#endregion Methods

	}

}
